package com.boda.common.util.http;

import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;

public class RequestHelper {

    public String getValue(HttpServletRequest request, String paramName) {
        String obj = request.getParameter(paramName);
        if (obj == null) {
            obj = "";
        }
        return obj.trim();
    }

    public String[] getValues(HttpServletRequest request, String paramName) {
        String[] value;
        value = request.getParameterValues(paramName);
        if (value == null)
            value = new String[] {};
        return value;
    }

    public String getValuesString(HttpServletRequest request, String paramName) {
        return getValuesString(request, paramName, ",");
    }

    public String getValuesString(HttpServletRequest request, String paramName, String delims) {
        String temp = "";
        String[] values = request.getParameterValues(paramName);
        if (values == null) {
            return "";
        } else {
            for (int i = 0; i < values.length; i++) {
                if (i == values.length - 1) {
                    temp += values[i].trim();
                } else {
                    temp += values[i].trim() + delims;
                }
            }
        }
        return temp;
    }

    public String getAjaxValue(HttpServletRequest request, String paramName) {
        String obj = request.getParameter(paramName);
        if (obj == null) {
            obj = "";
        }
        try {
            obj = java.net.URLDecoder.decode(obj, "UTF-8");
        } catch (Exception e) {
            obj = "";
        }
        return obj.trim();
    }

    public String[] getAjaxValues(HttpServletRequest request, String paramName) {
        String[] value, newValue;
        String tmp = "";
        value = request.getParameterValues(paramName);
        if (value == null)
            value = new String[] {};
        newValue = value;
        if (value.length > 0) {
            newValue = new String[value.length];

            try {
                for (int i = 0; i < value.length; i++) {
                    tmp = value[i];
                    tmp = java.net.URLDecoder.decode(tmp, "UTF-8");
                    newValue[i] = tmp;
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return newValue;
    }

    public String getAjaxValuesString(HttpServletRequest request, String paramName) {
        return getAjaxValuesString(request, paramName, ",");
    }

    public String getAjaxValuesString(HttpServletRequest request, String paramName, String delims) {
        String temp = "";
        String[] values = getAjaxValues(request, paramName);
        if (values == null) {
            return null;
        } else {
            for (int i = 0; i < values.length; i++) {
                if (i == values.length - 1) {
                    temp += values[i].trim();
                } else {
                    temp += values[i].trim() + delims;
                }
            }
        }
        return temp;
    }

}
